<?php
error_reporting(E_ALL ^ E_NOTICE);
session_start();
date_default_timezone_set('Asia/Bangkok');
$page_index = "index.php";

DEFINE('BASE', str_replace(array('\\', $page_index), array('/', ''), getenv('REQUEST_URI'))); 

DEFINE('BASEPATH',str_replace(array('\\', $page_index), array('/', ''), __FILE__));

DEFINE('TAILER', preg_replace('/^\//','', BASE));

DEFINE('CONPATH',BASEPATH.'Controller/');

DEFINE('MODPATH',BASEPATH.'Model/');

DEFINE('HELPATH',BASEPATH.'Helper/');

DEFINE('HELPATH',BASEPATH.'Helper/');

DEFINE('BODYPATH',BASEPATH.'View/');

DEFINE('APPPATH',BASEPATH.'Application/');

//$domain = explode('.',$_SERVER['HTTP_HOST']);
//
//$num = count($domain);
//
//if($num == 3){
//    $buff = array_pop($domain);
//    if($buff == 'com' || $buff == 'tv'){
//        DEFINE('DOMAIN',array_pop($domain));
//        DEFINE('DOMAINNAME',DOMAIN.'.'.$buff);
//        DEFINE('SUB',array_pop($domain));
//    }
//    else{
//        echo "DOMAIN IS ERROR";
//        exit();
//    }
//}
//else if($num == 2){
//    $buff = array_pop($domain);
//    if($buff == 'com' || $buff == 'tv'){
//        DEFINE('DOMAIN',array_pop($domain));
//        DEFINE('DOMAINNAME',DOMAIN.'.'.$buff);
//        DEFINE('SUB',null);
//    }
//    else{
//        DEFINE('DOMAIN',$buff);
//        DEFINE('DOMAINNAME',DOMAIN);
//        DEFINE('SUB',array_pop($domain));
//    }
//}
//else if($num == 1){
//    DEFINE('DOMAIN',array_pop($domain));
//    DEFINE('DOMAINNAME',DOMAIN);
//    DEFINE('SUB',null);
//}
//else{
//    echo "ERROR DOMAIN";
//    exit();
//}

require('Route/Route.php');
$route = new Route(TAILER);

if($route->uri[0] == 'Apps'){
    define('RPATH', 'Apps/'.$route->class.'/');
}
else{
    define('RPATH', CONPATH);
}

if (file_exists(RPATH.$route->class.'.php'))
{
  require_once(RPATH.$route->class.'.php');
  if (class_exists($route->class))
  {
    $obj = new $route->class(array(route => $route));
    if (method_exists($obj,$route->method))
    {
       $obj->{$route->method}($route->params);
    }
    else {echo "method not found.";}
  }
  else {echo "class not found.";}
}
else {echo "File class not found."; echo RPATH.$route->class.'.php';}
